package com.itbuka.goods.controller;


import com.itbuka.goods.domain.GoodsProductDetails;
import com.itbuka.goods.service.GoodsProductDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.itbuka.entity.Result;
import com.itbuka.entity.PageResult;
import com.github.pagehelper.Page;

import java.util.List;

/**
 * 商品详情表
 */
@RestController
@RequestMapping("/GoodsProductDetails")
public class GoodsProductDetailsController {

    @Autowired
    private GoodsProductDetailsService goodsProductDetailsService;

    /**
    * 查询全部商品详情表
    */
    @GetMapping("/selectAll")
    public Result< List<GoodsProductDetails> > selectAll(){
        return Result.ok("查询成功",goodsProductDetailsService.selectAll());
    }

    /**
     * 条件查询商品详情表
     */
    @PostMapping("/selectList")
    public Result< List<GoodsProductDetails> > select(@RequestBody GoodsProductDetails goodsProductDetails){
        return Result.ok("条件查询成功",goodsProductDetailsService.selectList(goodsProductDetails));
    }

    /**
     * 新增商品详情表
     */
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody GoodsProductDetails goodsProductDetails){
        goodsProductDetailsService.insert(goodsProductDetails);
        return Result.ok("新增成功");
    }

    /**
     * 删除商品详情表
     */
    @GetMapping("/delete")
    public Result<?> delete(@RequestParam String ids){
        goodsProductDetailsService.delete(ids);
        return Result.ok("删除成功");
    }

    /**
     * 修改商品详情表
     */
    @PostMapping("/update")
    public Result<?> update(@RequestBody GoodsProductDetails goodsProductDetails){
        goodsProductDetailsService.update(goodsProductDetails);
        return Result.ok("修改成功");
    }

    /**
    * 分页搜索实现商品详情表
    */
    @GetMapping(value = "/pageAll" )
    public Result<?> pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
    Page<GoodsProductDetails> pageList = goodsProductDetailsService.pageAll(page, size);
        PageResult<?> pageResult=new PageResult<>(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
    * 分页条件搜索实现商品详情表
    */
    @GetMapping(value = "/pageList" )
    public Result<?> pageList(@RequestBody GoodsProductDetails goodsProductDetails,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
    Page<GoodsProductDetails> pageList = goodsProductDetailsService.pageList(goodsProductDetails,page, size);
        PageResult<?> pageResult=new PageResult<>(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
     * 减库存
     */
    @GetMapping(value = "/reduceStock" )
    public Result<?> reduceStock(@RequestParam Long productId,@RequestParam Integer num){
        Integer i = goodsProductDetailsService.reduceStock(productId, num);
        if (i!=1){
            return Result.fail("减库存失败");
        }
        return Result.ok("减库存成功");
    }

}




